package com.sjm.bookshop.service.book;

import com.sjm.bookshop.dao.BookDao;
import com.sjm.bookshop.entity.BType;
import com.sjm.bookshop.entity.Book;
import com.sjm.bookshop.entity.BookS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class BookService {
    @Autowired
    private BookDao bookDao;

    //    查询所有图书类别
    public List<BType> getBooks() {
        return bookDao.typeList();
    }

    //    修改状态
    public int changeSta(int tid, int typestatus) {
        return bookDao.statuschange(tid, typestatus);
    }

    //    添加
    public int add(BType bType) {
        String b = bookDao.hasType(bType.getTypeId());
        System.out.println(b + bType.getTypename());
        if (b != null) {
//            不为空说明已经存在该类别了
            return 0;
        } else {
            bType.setAddtime(new Date());
            return bookDao.addT(bType);
        }
    }

    //    删除
    public int del(int tid) {
        return bookDao.delT(tid);
    }

    //    模糊查询
    public List<BType> select(String keyWord) {
        return bookDao.selectLike(keyWord);
    }

    //    查询所有图书
    public List<BookS> selectAll() {
        List<BookS> bookS = bookDao.selectBookAndTypeName();

//        return bookDao.selectBooks();
        return bookS;
    }

    //    添加图书
    public int addBook(Book book){
        book.setAddtime(new Date());
        return bookDao.addB(book);
    }

//    删除
    public int deleteBook(int bookId){
        return bookDao.del(bookId);
    }
//    修改
    public int changeBook(int bookId,int ison){
        return bookDao.changeB(bookId,ison);
    }
//  推荐
    public int changeBookisRecommend(int bookId, int isrecommend) {
        return bookDao.changeR(bookId,isrecommend);
    }
//    更新
    public int updateBooks(Book book){
        book.setUpdatetime(new Date());
        return bookDao.updateB(book);
    }

    public List<BookS> searchByKeyWord(String keyWord) {
        return bookDao.search(keyWord);
    }
}
